package com.seo.controller.proxy.user;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seo.pojo.ReturnBody;
import com.seo.service.proxy.sdk.ProxyUserService;
import com.seo.utils.ClientConst;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * maincontroller <br/>
 * 
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/proxy/user")
public class ProxyUserController {

	@Autowired
	private ProxyUserService proxyUserService;

	@RequestMapping
	public String index(HttpServletRequest request, HttpSession session) {
		log.info("进入代理客户详情...");
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_PROXY_USER);
		Map<String, Object> user = proxyUserService.getProxyUserById(userId);

		request.setAttribute("user", user);

		return "proxy/user/userInfo";

	}

	@RequestMapping("/save")
	@ResponseBody
	public ReturnBody save(@RequestParam Map<String, Object> params, HttpSession session) {
		log.info("保存用户信息...");
		ReturnBody rb = ReturnBody.init();
		if (!params.containsKey("userId")) {
			log.error("用户不存在");
			return rb.error("用户不存在");
		}
		String id = (String) params.get("id");
		if (!StringUtils.isNumeric(id)) {
			log.error("用户ID不是数字");
			return rb.error("用户不存在");
		}

		Integer curUserId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_PROXY_USER);
		if (Integer.parseInt(id) != curUserId) {
			log.error("只能修改个人信息");
			return rb.error("用户不存在");
		}
		params.put("userId", Integer.parseInt((String) params.get("userId")));

		return proxyUserService.updateProxyUser(params);
	}

	@RequestMapping("/toUpdatePwd")
	public String toUpdatePwd(){
		return "proxy/user/updatePwd";
	}
	
	@RequestMapping("/updatePwd")
	@ResponseBody
	public ReturnBody updatePwd(@RequestParam Map<String, Object> params, HttpSession session) {
		log.info("修改用户密码...");

		Integer id = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_PROXY_USER);

		params.put("id", id);

		return proxyUserService.updateProxyUserPwd(params);
	}
	
}
